/**
*      author: Y_Yao
*      created: 2024-11-29 13:36:52
**/

#include<bits/stdc++.h>

using namespace std;
#define int LL
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
const int N = 2e4 + 7;
int dp[N][2];
int r[N],l[N];
int n;

void solve()
{
    cin >> n;
    for(int i = 1;i <= n;i++)
    {
        cin >> l[i] >> r[i];
    }

    dp[1][0] = r[1] - 1 + r[1] - l[1];
    dp[1][1] = r[1] - 1;

    for(int i = 2;i <= n;i++)
    {
        dp[i][0] = min(dp[i-1][0] + abs(r[i] - l[i-1]) + r[i] - l[i] + 1,dp[i-1][1] + abs(r[i] - r[i-1]) + r[i] - l[i] + 1);
        dp[i][1] = min(dp[i-1][0] + abs(l[i] - l[i-1]) + r[i] - l[i] + 1,dp[i-1][1] + abs(l[i] - r[i-1]) + r[i] - l[i] + 1);
    }

    cout << min(dp[n][0] + n - l[n],dp[n][1] + n - r[n]) << endl;;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}